package com.iqiyi.pps.epg.core.dao.base;

import com.iqiyi.kiwi.cache.Cacheable;
import com.iqiyi.kiwi.cache.utils.CacheUtil;
import com.iqiyi.kiwi.event.Event;
import com.iqiyi.kiwi.event.annotation.EventListener;
import com.iqiyi.pps.epg.core.model.base.BaseEntity;
import org.springframework.stereotype.Component;

/**
 * 通用EntityDao
 * User: lishunlong
 * Date: 13-5-25
 * Time: 下午4:17
 * To change this template use File | Settings | File Templates.
 */
@Component
public class BaseEntityDao extends BaseDao<BaseEntity> {

    /**
     * 实体更新时，刷新缓存
     * @param entity
     */
    @EventListener(value = Event.EVENT_TYPE_ENTITY_UPDATE)
    public void saveToCache(final BaseEntity entity) {
        if (entity instanceof Cacheable) {
            Class rootClass = entity.getClass();
            com.iqiyi.kiwi.cache.Cache cache = cacheManager.getObjectCache(rootClass.getSimpleName());
            if (cache != null) {
                String cacheKey = CacheUtil.generateItemKey((Cacheable) entity);
                cache.put(cacheKey, entity);
            }
            logger.debug("save entity to cache: {}", entity);
        }
    }
}
